/*
 * @Description: 浏览器打印通用文件
 * @Date: 2022-03-25 12:03:44
 * @Author: yeming
 */

// olcsMapLog(window) window作为参数可传可不传
// 使用时正常 console.log(123) 只不过会加上 OLCS_MAP: 实际打印结果为 OLCS_MAP: 123
; (function olcsMapLog (w) {
  w = !w && w ? w : window
  var c = w.console;
  var OLCSMAP = 'OLCS_MAP: ';
  // w.prod = true; // 打开则为线上 表示禁止打印一切日志
  if (c) {
    var log = c.log;
    c.log = function () {
      if (w.prod) return;

      if (arguments[0] && arguments[0] === OLCSMAP) {
        return;
      } else {
        Array.prototype.unshift.call(arguments, OLCSMAP);
      }
      if (log) {
        log.apply(c, arguments);
      } else {
        document.write.apply(c, arguments);
      }
    };

    var info = c.info;
    c.info = function () {
      if (w.prod) return;
      Array.prototype.unshift.call(arguments, OLCSMAP);
      if (info) {
        info.apply(c, arguments);
      } else {
        c.log.apply(c, arguments)
      }
    }

    var debug = c.debug;
    c.debug = function () {
      if (w.prod) return;
      Array.prototype.unshift.call(arguments, OLCSMAP);
      if (debug) {
        debug.apply(c, arguments);
      } else {
        c.log.apply(c, arguments)
      }
    }

    var warn = c.warn;
    c.warn = function () {
      if (w.prod) return;
      Array.prototype.unshift.call(arguments, OLCSMAP);
      if (warn) {
        warn.apply(c, arguments);
      } else {
        c.log.apply(c, arguments)
      }
    }

    var error = c.error;
    c.error = function () {
      if (w.prod) return;
      Array.prototype.unshift.call(arguments, OLCSMAP);
      if (error) {
        error.apply(c, arguments);
      } else {
        c.log.apply(c, arguments)
      }
    }

    var trace = c.trace;
    c.trace = function () {
      if (w.prod) return;
      Array.prototype.unshift.call(arguments, OLCSMAP);
      if (trace) {
        trace.apply(c, arguments);
      } else {
        c.log.apply(c, arguments)
      }
    }

    var assert = c.assert;
    c.assert = function () {
      if (w.prod) return;
      if (assert) {
        assert.apply(c, arguments);
      } else if (!arguments[0]) {
        c.error.apply(c, arguments);
      }
    }

    var count = c.count;
    c.count = function () {
      if (w.prod) return;
      if (count) {
        count.apply(c, arguments);
      } else {
        var num = this.num || 0;
        num = num + 1;
        this.num = num;
      }
    };

    c.reset = function () {
      this.count = 0;
      w.prod = false;
    }
  }
})(this);

export {
  olcsMapLog
}